package com.foogui.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class HelloCglibProxy implements MethodInterceptor {

    @Override
    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        before();
        Object res = methodProxy.invokeSuper(o, args);
        after();
        return res;
    }

    public void before() {
        System.out.println("before");
    }
    public void after() {
        System.out.println("after");
    }

    public static void main(String[] args) {
        HelloImplWithoutInterface hello = new HelloImplWithoutInterface();
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(hello.getClass());
        enhancer.setCallback(new HelloCglibProxy());
        HelloImplWithoutInterface o = (HelloImplWithoutInterface)enhancer.create();
        o.say("66");
        o.say2("66");
    }
}
